<?php
class Model_Block_Block extends Model_System {
	public $_name = "block";
	protected $_sequence = true;
	public function getAll() {
		$result = $this->select();
		return $this->fetchAll($result)->toArray();
	}
	public function getAllStartWith($char) {
		$result = $this->select();
		$result->where('name like ?','%'.$char.'%');
		$result->order("id DESC");
		return $this->fetchAll($result)->toArray();
	}
	public function getNumbSub($id) {
	    $item = $this->getItemId($id);
	    if($item['parent_id']==1)
	        return 0;
	    else
	    {
	        $temp = $this->getNumbSub($item['parent_id']);
	        return $temp + 1;
	    }
	}
	public function getAllIsActive() {
		$result = $this->select();
		$result->where('status = 1');
		return $this->fetchAll($result)->toArray();
	}
	public function getAllIsActiveExceptId($id) {
		$result = $this->select();
		$result->where('id != ?',$id);
		$result->where('status = 1');
		return $this->fetchAll($result)->toArray();
	}
	public function getItemId($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		return $this->fetchRow($result)->toArray();
	}
	public function getItemAlias($alias)
	{
		$result = $this->select();
		$result->where('alias = ?',$alias);
		return $this->fetchRow($result)->toArray();
	}
	static function getNamebyId($id)
	{
		$excute = new Model_Block_Block();
		$query = $excute->select();
		$query->where('id = ?',$id);
		$result = $excute->fetchRow($query)->toArray();
		return $result['name'];
	}
	public function getItemsLanguage($lang){
		$result = $this->select();
		$result->where('language = ?',$lang);
		return $this->fetchAll($result)->toArray();
	}
	public function changeStatus($id)
	{
		$query = $this->select();
		$query->where('id = ?',$id);
		$result = $this->fetchRow($query)->toArray();
		if($result['status']=='1')
		{
			$data = array('status'=>'0'); 
			$this->update($data, 'id = '.$id);
		}
		else
		{
			$data = array('status'=>'1'); 
			$this->update($data, 'id = '.$id);
		}
	}
	//Kiểm tra id có sub cate ko ?
	public function checkHaveSub($id)
	{
		$query = $this->select();
		$query->where('parent_id = ?',$id);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	//Kiểm tra id có chứa file nào không ?
	public function checkHaveFile($id)
	{
	    $modelFile = new Model_File_File();
		$query = $modelFile->select();
		$query->where('category = ?',$id);
		$result = $modelFile->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	public function checkImage($image)
	{
		$query = $this->select();
		$query->where('image = ?',$image);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	public function insertItem($data)
	{
		try {
		    $this->insert($data);
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
		
	}
	public function getListParent($id)
	{
		$Cat = $this->select();
		$Cat->where('id = ?',$id);
		$dataCat = $this->fetchRow($Cat)->toArray();
		$data = array();
		if($dataCat['parent_id']!='1')
		{
			$data[] = $id;
			$data = array_merge($data,$this->getListParent($dataCat['parent_id']));
		}
		else 
		{
			$data[] = $id;
		}
		return $data;
		
		
	}
	public function deleteItem($id)
	{
		$model = new Model_Article_Article();
		$model->update(array('category'=>'1'),'category = '.$id);
		$model->changeStatusByCater($id);
		
		//Tìm kiếm các item có parent = id
		$query = $this->select();
		$query->where('parent_id = ?',$id);
		$data = $this->fetchAll($query)->toArray();
		foreach ($data as $item)
		{
			$dataTemp = array('parent_id'=>'1');
			$this->update($dataTemp,'id = '.$item['id']);
		}
		$this->delete('id = '.$id);
	}
    public function updateItem($data,$id){
		try {
		    $this->update($data, "id = $id");
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
	}
}